<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true" align-center draggable overflow>
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-form-item label="分类" prop="apply_cate">
                <div class="SeletBox">
                    <el-select v-model="form.apply_cate" filterable style="width: 100%">
                        <el-option v-for="item in cateList" :key="item.apply_cate_id" :label="item.apply_cate_name" :value="item.apply_cate_id" />
                    </el-select>
                    <el-button type="primary" icon="el-icon-plus" v-auth="'applyCate.add'" @click="addData('cateSave', 'cateSaveDialog')"></el-button>
                </div>
            </el-form-item>
            <el-form-item label="类型" prop="type">
                <el-select v-model="form.type " filterable style="width: 100%" @change="getAccount">
                    <el-option label="供应商" :value="1" />
                    <el-option label="染色厂" :value="2" />
                    <el-option label="加工厂" :value="3" />
                </el-select>
            </el-form-item>
            <el-form-item label="标签" prop="label">
                <el-select v-model="form.label " filterable style="width: 100%">
                    <el-option label="正单" value="正单" />
                    <el-option label="处理" value="处理" />
                </el-select>
            </el-form-item>
            <el-form-item label="供应商名称" prop="apply_name">
                <el-input v-model="form.apply_name" placeholder="供应商名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="关联账号" prop="admin_id" v-if="!form.admin_id">
                <el-select v-model="form.admin_id" filterable clearable placeholder="请选择关联账号" style="width:100%">
                    <el-option v-for="item in accountList" :key="item.id" :label="item.account" :value="item.id"/>
                </el-select>
            </el-form-item>
            <el-form-item label="联系人" prop="linkman">
                <el-input v-model="form.linkman" placeholder="联系人" clearable></el-input>
            </el-form-item>
            <el-form-item label="联系电话" prop="mobile">
                <el-input v-model="form.mobile" placeholder="联系电话" clearable></el-input>
            </el-form-item>
            <el-form-item label="联系地址" prop="address">
                <el-input v-model="form.address" placeholder="联系地址" clearable></el-input>
            </el-form-item>
            <el-form-item label="业务员" prop="sales_man" v-if="form.type == 1">
                <el-select v-model="form.sales_man" placeholder="请选择" filterable clearable>
                    <el-option v-for="item in adminList" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="仓库" prop="ware_id">
                <el-select v-model="form.ware_id" placeholder="请选择" filterable clearable>
                    <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="system_disable">
                <el-switch v-model="form.system_disable" :active-value="0" :inactive-value="1" inline-prompt active-text="启用" inactive-text="禁用"></el-switch>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
	<cate-save-dialog v-if="dialog.cateSave" ref="cateSaveDialog" @success="getCateList" @closed="dialog.cateSave = false"></cate-save-dialog>
</template>

<script>
import cateSaveDialog from "./cateSave.vue";

export default {
	components: { cateSaveDialog },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增供应商',
				edit: '编辑供应商',
				show: '查看供应商'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				apply_id: "",
                apply_cate: "",
                type: 1,
                label: "",
                ware_id: "",
                apply_name: "",
                linkman: "",
                mobile: "",
                address: "",
				system_disable: 0,
                sales_man: "",
                admin_id: "",
			},
			cateList: [],
			grandList: [],
			//验证规则
			rules: {
				apply_cate: [{ required: true, message: '请选择供应商分类' }],
				apply_name: [{ required: true, message: '请输入供应商名称' }],
				type: [{ required: true, message: '请选择类型' }],
				// admin_id: [{ required: true, message: '请关联账号' }],
			},
			gradeList: [],
            wareList: [],
			dialog: {
				cateSave: false
			},
			adminList: [],
			accountList: [],
		}
	},
	mounted() {
		this.getWare();
		this.getCateList();
		this.getAdmin()
	},
	methods: {
		getPid(data) {
			if (data.apply_cate_id) {
				this.form.apply_cate = data.apply_cate_id
			}
		},
		/**添加点击*/
		addData(dialog, ref) {
			this.dialog[dialog] = true
			this.$nextTick(() => {
				this.$refs[ref].open()
			})
		},
		async getCateList() {
			var res = await this.$API.basic.applyCate.all.get();
			this.cateList = res.data;
			if (this.cateList.length > 0 && (this.mode == 'add') && !this.form.apply_cate) {
				this.form.apply_cate = this.cateList[0].apply_cate_id
			}
		},
        async getAdmin() {
			var res = await this.$API.system.admin.all.get(
                {apply_type: 10}
            );
			this.adminList = res.data
		},
        async getAccount() {
			var res = await this.$API.system.admin.all.get(
                {apply_type: this.form.type}
            );
			this.accountList = res.data
		},
        async getWare() {
			var res = await this.$API.basic.ware.all.get();
			this.wareList = res.data
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.apply_id) {
						res = await this.$API.basic.apply.edit.post(this.form);
					} else {
						res = await this.$API.basic.apply.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success('操作成功')
					} else {
						this.$alert(res.message, '提醒', { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.apply_id = data.apply_id
			this.form.apply_cate = data.apply_cate
			this.form.label = data.label
			this.form.type = data.type
			this.form.ware_id = Number(data.ware_id)
			this.form.apply_name = data.apply_name
			this.form.linkman = data.linkman
			this.form.mobile = data.mobile
			this.form.address = data.address
			this.form.system_disable = data.system_disable
			this.form.sales_man = data.sales_man
			this.form.admin_id = data.admin_id
		    this.getAccount()
		}
	}
}
</script>

<style></style>
